/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：BLGFastRenderView.java <br>
 * 创建时间：2013-5-19 下午10:37:35 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.blgframework.impl;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.LoadingScreen;
import lostland.gmud.exv2.ui.core.ButtonControlledScreen;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 类名：BLGFastRenderView <br>
 * 说明：
 *
 * @author 12548
 */
@SuppressLint("ViewConstructor")
public class BLGFastRenderView extends SurfaceView implements Runnable {
	Game game;
	Bitmap framebuffer;
	Thread renderThread = null;
	SurfaceHolder holder;
	volatile boolean running = false;


	public static float multiplier = 1f;

	public BLGFastRenderView(BLGGame game, Bitmap framebuffer) {
		super(game);
		this.game = (Game) game;
		this.framebuffer = framebuffer;
		holder = getHolder();
	}

	public void resume() {
		running = true;
		renderThread = new Thread(this);
		renderThread.start();
	}


	float frameTime = 0;
	int frameCount = 0;
	int fps = 0;


	@Override
	public void run() {
		Rect dstRect = new Rect();
		long startTime = System.nanoTime();
		while (running) {
			if (holder == null) {
				holder = getHolder();
			}
			if (!holder.getSurface().isValid()) {
				continue;
			}
			float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
			//10秒1天
			startTime = System.nanoTime();

			framebuffer.eraseColor(0);
//			game.mapFB.eraseColor(0);
			game.getCurrentScreen().render(deltaTime);
			game.getCurrentScreen().present(deltaTime);



			Canvas canvas = holder.lockCanvas();
			canvas.getClipBounds(dstRect);

//			Rect newRect = new Rect(dstRect.left,dstRect.top,(int) ((dstRect.right - dstRect.left) * multiplier + dstRect.left),(int) ((dstRect.bottom - dstRect.top) * multiplier + dstRect.top));

//			int mapx = 32 - game.mapDX;
//			int mapy = 32 - game.mapDY;
//
			canvas.drawRGB(0, 0, 0);
//
//			if(game.drawMap){
////				Gmud.ms.updateMapBmp(Gmud.ms.getMap(), Gmud.ms.bmp);
//				final int bottom = mapy + 32 * GameConstants.RES_H[GmudGame.resolution_level];
//				final int right = mapx + 32 * GameConstants.RES_W[GmudGame.resolution_level];
//				final Rect mapRect = new Rect(mapx,mapy,right,
//						bottom);
////				Paint p = new Paint();
////				p.setColor(Gmud.getBgColor());
////				canvas.drawRect(mapRect, p);
//				canvas.drawBitmap(
//						Gmud.ms.frontBuffer.bitmap,
//						mapRect,
//						dstRect, null);
//			}



//			if(game.drawMap) {
//				canvas.drawBitmap(MapBuffer.getInstance().getBufferedMap(), null, dstRect, null);
//			}
			canvas.drawBitmap(game.mapFB, null, dstRect, null);
			canvas.drawBitmap(framebuffer, null, dstRect, null);

			if (game.getCurrentScreen() instanceof ButtonControlledScreen) {
				canvas.drawBitmap(game.SecondFB, null, dstRect, null);
			} else if (game.getCurrentScreen() instanceof LoadingScreen) {
				if (LoadingScreen.b2) {
					canvas.drawBitmap(((BLGPixmap) ((LoadingScreen) game.getCurrentScreen()).loading2).bitmap, null, dstRect, null);
				} else {
					canvas.drawBitmap(((BLGPixmap) ((LoadingScreen) game.getCurrentScreen()).loading1).bitmap, null, dstRect, null);
				}

			} else {
//				game.SecondFB.eraseColor(0);
			}

			frameCount++;
			frameTime += deltaTime;
			if(frameTime >= 1.0f) {
				frameTime -= 1.0f;
				fps = frameCount;
				frameCount = 0;
			}

			Paint p = new Paint();
			p.setTextSize(48);
			p.setColor(0xFFFFFF);
			p.setAlpha(255);

			canvas.drawText("FPS : " + fps, 50, 50, p);

			holder.unlockCanvasAndPost(canvas);
		}
	}

	public void pause() {
		running = false;
		while (true) {
			try {
				renderThread.join();
				break;
			} catch (InterruptedException e) {
				// retry
			}
		}
	}
}
